// 模块：入库，出库，库存

import axios from 'axios'

const modelUrl = '/produce'

// 库存
export interface StockReq {
  stockWarningQty?: number // 库存预警数量
  productID?: string
  supplierId?: string
  materialName: string
  materialCategory: string
  spec: string
  materialCode: string
  materialDesc: string
  fluteType: string
  boxSpec: string
  crimpLine: string
  unit: string
  storageArea: string
  materialType: number
  stockQty: number
  unitPrice: number
  // 增加的字段
  remark: string
  materialStockId?: string
  amount: number
}

export interface StockRes {
  currentPage?: number
  pageSize?: number
  totalPage?: number
  totalCount?: number
  list: StockResItem[]
}

export interface StockResItem {
  amount: string
  materialStockId?: string
  supplierId?: string
  materialName?: string
  materialCategory?: string
  spec?: string
  materialCode?: string
  materialDesc?: string
  fluteType?: string
  boxSpec?: string
  crimpLine?: string
  unit?: string
  storageArea?: string
  createdDt?: string
  materialType?: number
  stockQty?: number
  unitPrice?: number
  supplierName?: string
  index?: number // 导出的序号
  stockWarningQty: number // 库存预警数量
}

export interface StockSearch {
  supplierName?: string
  materialName?: string
  spec?: string
  materialType?: number
  materialCategory?: string
  materialCode?: string
  materialDesc?: string
  fluteType?: string
  boxSpec?: string
  crimpLine?: string
  unit?: string
  storageArea?: string
  materialStockId?: number
  supplierId?: string
  stockQty?: number
  unitPrice?: number
  tenantId?: number
  createTime?: string[]
  createdStartDate?: string
  createdEndDate?: string
  orderBy?: number
  isWarned?: number // 是否警告
}

export function editStock(data: StockReq) {
  return axios.post<StockRes>(modelUrl + '/materialStockAdit', data)
}

export function delStock(data: { materialStockId: string }) {
  return axios.post<object>(modelUrl + '/materialStockDel', data)
}

export function getStockDetail(data: { materialStockId: string }) {
  return axios.post<StockRes>(modelUrl + '/materialStockDetail', data)
}

export function getStockAllList(data?: StockSearch) {
  return axios.post<StockRes>(modelUrl + '/materialStockList', data)
}

export function getStockPageList(data?: StockSearch) {
  return axios.post<StockRes>(modelUrl + '/materialStockPage', data)
}

export function batchDelStock(data?: { idList: string[] }) {
  return axios.post<StockRes>(modelUrl + '/materialStockDelBatch', data)
}

// 出入库
export interface StockRecordReq {
  supplierId?: string
  materialStockId?: string
  orderCode?: string
  remark?: string
  formType?: number
  inboundQty?: number
  outboundQty?: number
  recordType?: number
  stockRecordId?: string
  recordId?: string
  materialName?: string
  materialCategory?: string
  spec?: string
  materialCode?: string
  materialDesc?: string
  fluteType?: string
  boxSpec?: string
  crimpLine?: string
  unit?: string
  storageArea?: string
  materialType?: number
  stockQty?: number
  unitPrice?: number
  outStockQty?: number
  companyId?: string
}

export interface StockRecordRes {
  currentPage?: number
  pageSize?: number
  totalPage?: number
  totalCount?: number
  list: StockRecordResItem[]
}

export interface StockRecordResItem {
  record?: boolean
  boxSpec?: string
  createdDt?: string
  crimpLine?: string
  fluteType?: string
  materialCategory?: string
  materialCode?: string
  materialDesc?: string
  materialName?: string
  materialStockId: string
  orderCode?: string
  remark?: string
  spec?: string
  stockRecordId?: string
  supplierId?: string
  supplierName?: string
  unit?: string
  formType?: number
  materialType?: number
  inboundQty?: number
  outboundQty?: number
  stockQty?: number
  index?: number // 导出的序号
  recordType: number
  recordId: string
  storageArea: string
  unitPrice: number
}

export interface StockRecordSearch {
  customerName?: string
  taxNo?: string
  contactName?: string
  formType?: number
  orderBy?: number

  // 需要添加的子段
  // 订单编号
  // 产品名称
  companyName?: string
  supplierName?: string
  spec?: string
  materialType?: number
  materialCategory?: string
  materialCode?: string
  materialDesc?: string
  fluteType?: string
  boxSpec?: string
  crimpLine?: string
  unit?: string
  storageArea?: string
  materialStockId?: number
  supplierId?: string
  stockQty?: number
  unitPrice?: number
  tenantId?: number
  create?: string[]
  createdStartDate?: string
  createdEndDate?: string
  materialName?: string
  orderCode?: string
}

export function editStockRecord(data: StockRecordReq) {
  return axios.post<StockRecordRes>(modelUrl + '/materialStockRecordAdit', data)
}

export function delStockRecord(data: { stockRecordId: string }) {
  return axios.post<object>(modelUrl + '/materialStockRecordDel', data)
}

export function getStockRecordDetail(data: { stockRecordId: string }) {
  return axios.post<StockRecordResItem>(modelUrl + '/materialStockRecordDetail', data)
}

export function getStockRecordAllList(data: StockRecordSearch) {
  return axios.post<StockRecordRes>(modelUrl + '/materialStockRecordList', data)
}

export function getStockRecordPageList(data: StockRecordSearch) {
  return axios.post<StockRecordRes>(modelUrl + '/materialStockRecordPage', data)
}

export function batchAddStockRecord(data: StockRecordReq[]) {
  return axios.post<StockRecordRes>(modelUrl + '/materialStockRecordBatchAdd', data)
}

export function batchDelStockRecord(data?: { idList: string[] }) {
  return axios.post<StockRes>(modelUrl + '/materialStockRecordDelBatch', data)
}
